var px, py, sx, sy;
var list = [];
var lastbutton = null;
var lastFrame = null;
var inBtn = false;
var inFrame = false;
var isAltKey = false;
var isPlayAudio = false;
var huaci_switch, huaci_button;
if (document.body.addEventListener) {
    document.body.addEventListener("mouseup", onEventTranslate, false);
    document.body.addEventListener("mousedown", getHuaciSwitch, false);
} else {
    document.body.attachEvent("onmouseup", onEventTranslate);
    document.body.attachEvent("onmousedown", getHuaciSwitch);
}
function getHuaciSwitch() {
    chrome.storage.local.get(null,
    function(a) {
        huaci_switch = a.huaci_switch;
        huaci_button = a.huaci_button;
    });
}
function hasSpaceCount(a) {
    var c = 0;
    for (var b = 0; b < a.length; b++) {
        if (a.charAt(b) == " ") {
            c++;
        }
    }
    if (c > 0) {
        return true;
    } else {
        return false;
    }
}
function isAllChinese(a) {
    var b = /^[\u4e00-\u9fa5]+$/;
    if (b.test(a)) {
        return true;
    } else {
        return false;
    }
}
function isEnglish(a) {
    var c = /[a-zA-Z]/;
    for (var b = 0; b < a.length; b++) {
        if (!c.test(a[b])) {
            return false;
        }
    }
    return true;
}
function onEventTranslate(c) {
    var b = "";
    var a = c.srcelement ? c.srcelement: c.target;
    if (document.getSelection) {
        b = document.getSelection();
    } else {
        if (document.selection) {
            b = document.selection.createRange().text;
        }
    }
    if (inBtn) {
        return;
    }
    if (inFrame) {
        return;
    }
    onCloseFrame();
    onClosebutton();
    if (!huaci_switch) {
        return;
    }
    b = String(b);
    b = b.replace(/^\s*/, "").replace(/\s*$/, "");
    if (b == "") {
        return;
    }
    if (a.tagName == "INPUT" || a.tagName == "IMG") {
        return;
    }
    if (b.length > 500) {
        return;
    }
    if (isAllChinese(b)) {
        return;
    }
    if (b.indexOf("<") == 1 || b.indexOf(">") == 1) {
        return;
    }
    if (b !== "") {
        px = c.pageX;
        py = c.pageY;
        sx = c.screenX;
        sy = c.screenY;
        if (!hasSpaceCount(b)) {
            if (isEnglish(b)) {
                getDictTrans(b, c.pageX, c.pageY, c.screenX, c.screenY);
            } else {
                getTranslate(b, c.pageX, c.pageY, c.screenX, c.screenY);
            }
        } else {
            getTranslate(b, c.pageX, c.pageY, c.screenX, c.screenY);
        }
    }
}
function getDictTrans(d, b, a, e, c) {
    chrome.extension.sendRequest({
        action: "dict",
        word: d
    },
    function(f) {
        var g = f.data;
        if (g.data == "" || g.data == null) {
            getTranslate(d, b, a, e, c);
        } else {
            if (huaci_button) {
                createFyBtn(b, a, e, c);
                $("#fanyiWrapper").click(function() {
                    createDictFrame(d, b, a, e, c, g);
                    $(this).hide();
                });
            } else {
                createDictFrame(d, b, a, e, c, g);
            }
        }
    });
}
function getTranslate(d, b, a, e, c) {
    chrome.extension.sendRequest({
        action: "trans",
        word: d
    },
    function(f) {
        var g = f.data;
        if (huaci_button) {
            createFyBtn(b, a, e, c);
            $("#fanyiWrapper").click(function() {
                createFyFrame(d, b, a, e, c, g);
                $(this).hide();
            });
        } else {
            createFyFrame(d, b, a, e, c, g);
        }
    });
}
function createDictFrame(q, c, b, e, d, t) {
    var u = "";
    var s = "";
    var a = $("<div></div>");
    a.attr("id", "fanyiContainer");
    var l = t.data.symbols[0].parts;
    if (l.length == 0) {
        return;
    }
    if (l.length > 3) {
        l = l.slice(0, 3);
    }
    for (var p = 0; p < l.length; p++) {
        for (var o = 0; o < l[p]["means"].length; o++) {
            s += "<span>" + l[p]["means"][o] + ";</span>";
        }
        u += '<p style="font-family:\'微软雅黑\';margin-top:2px;margin-bottom:0;line-height:18px;font-size:13px;"><span style="width:40px;text-overflow:ellipsis;margin-right:5px;">' + l[p]["part"] + "</span>" + s + "</p>";
        s = "";
    }
    a[0].innerHTML = '<div style="padding:13px 13px;width:257px;border:1px solid #ccc;box-shadow:0 0 3px #ccc;border-radius:2px;background:#fff;text-align:left;"><div style="height:17px;"><span style="font-size:14px;display:inline-block;font-family:\'微软雅黑\';">' + t.data["word_name"] + '</span><audio src="http://tts.baidu.com/text2audio?lan=en&pid=101&ie=UTF-8&text=' + t.data["word_name"] + '" visibility:hidden;></audio><img id="playVoice" src="' + chrome.extension.getURL("imgs/map/voice.png") + '" style="vertical-align:baseline;margin-left:7px;display:inline-block;position:relative;top:3px;left:0;cursor:pointer;width:18px;height:15px;"><img id="closeFrame" style="float:right;" src="' + chrome.extension.getURL("imgs/map/close.png") + '"></div><div style="margin-top:8px;">' + u + '</div><div style="margin-top:8px;"><img src="' + chrome.extension.getURL("imgs/map/graylogo.png") + '" style="display:inline-block;"><a id="moreMean" target="_blank" href="http://fanyi.baidu.com/#en/zh/' + t.data["word_name"] + '" style="font-family:\'微软雅黑\';float:right;text-decoration:none;color:#0066FF;letter-spacing:1px;font-size:12px;position:relative;top:4px;left:0;">更多释义 ></a></div></div>';
    var n = 310;
    var g = 100;
    var f = 0;
    var k = 0;
    var m = screen.availWidth;
    var r = screen.availHeight;
    var h = 10;
    if (e + n < m) {
        f = c;
    } else {
        f = c - n - 20;
    }
    a[0].style.left = f + "px";
    if (d + g + 20 < r) {
        k = b;
    } else {
        k = b - g - 20;
    }
    a[0].style.top = k + 10 + "px";
    a[0].style.position = "absolute";
    a[0].style.zIndex = 10002;
    document.body.style.position = "static";
    document.body.appendChild(a[0]);
    list.push(a);
    $("#fanyiContainer").mouseover(function() {
        inFrame = true;
    }).mouseout(function() {
        inFrame = false;
    });
    $("#closeFrame").hover(function() {
        $(this).attr("src", chrome.extension.getURL("imgs/map/close_hover.png"));
    },
    function() {
        $(this).attr("src", chrome.extension.getURL("imgs/map/close.png"));
    });
    if (lastFrame != null) {
        if (lastFrame.css("left") !== $button.css("left")) {
            document.body.removeChild(lastFrame[0]);
        }
    }
    lastFrame = a;
    $("#closeFrame").click(function() {
        inFrame = false;
        $("#fanyiContainer").hide();
    });
    $("#moreMean").click(function() {
        inFrame = false;
        $("#fanyiContainer").hide();
    });
    $("#playVoice").hover(function() {
        if (!isPlayAudio) {
            $(this).attr("src", chrome.extension.getURL("imgs/map/voice_hover.png"));
        }
    },
    function() {
        if (!isPlayAudio) {
            $(this).attr("src", chrome.extension.getURL("imgs/map/voice.png"));
        }
    });
    $("#playVoice").click(function() {
        var j = this;
        isPlayAudio = true;
        $(this).siblings("audio")[0].play();
        var i = $(this).siblings("audio");
        i[0].play();
        $(this).attr("src", chrome.extension.getURL("imgs/map/sound.gif"));
        i.unbind("ended").bind("ended",
        function() {
            isPlayAudio = false;
            $(j).attr("src", chrome.extension.getURL("imgs/map/voice.png"));
        });
    });
}
function createFyFrame(q, c, b, e, d, s) {
    var t = "";
    var l = "";
    var j = s.trans_result;
    var a = $("<div></div>");
    a.attr("id", "fanyiContainer");
    var h = document.createElement("div");
    for (var p = 0; p < j.length; p++) {
        t += j[p]["dst"];
    }
    for (var p = 0; p < j.length; p++) {
        l += j[p]["src"];
    }

    h.innerHTML = '<div style="padding:13px 13px;width:257px;border:1px solid #ccc;border-radius:2px;box-shadow:0 0 5px #ccc;background:#fff;text-align:left;font-family:\'微软雅黑\';"><div><span style="font-size:13px;display:inline-block;font-family:\'微软雅黑\';">译文:</span><audio src="http://tts.baidu.com/text2audio?lan=en&pid=101&ie=UTF-8&text=' + l + '" visibility:hidden;></audio><img id="playVoice" src="' + chrome.extension.getURL("imgs/map/voice.png") + '" style="vertical-align:baseline;margin-left:7px;display:inline-block;position:relative;top:3px;left:0;cursor:pointer;width:18px;height:15px;"><img style="float:right;" id="closeFrame" src="' + chrome.extension.getURL("imgs/map/close.png") + '"></div><p id="content" style="white-space:normal;margin-top:8px;font-size:13px;font-family:\'微软雅黑\';color:#333;padding:0;line-height:18px;width:257px;">' + t + '</p><div style="margin-top:8px;"><img src="' + chrome.extension.getURL("imgs/map/graylogo.png") + '" style="display:inline-block;"><a id="moreMean" target="_blank" href="http://fanyi.baidu.com/#auto/zh/' + l + '" style="float:right;text-decoration:none;color:#0066FF;letter-spacing:1px;font-size:12px;font-family:\'微软雅黑\';position:relative;left:0;top:5px;">更多释义 ></a></div></div>';
    a[0].appendChild(h);
    var o = 310;
    var g = 100;
    var f = 0;
    var m = 0;
    var n = screen.availWidth;
    var r = screen.availHeight;
    var k = 10;
    if (e + o < n) {
        f = c;
    } else {
        f = c - o - 20;
    }
    a[0].style.left = f + "px";
    if (d + g + 20 < r) {
        m = b;
    } else {
        m = b - g - 20;
    }
    a[0].style.top = m + 10 + "px";
    a[0].style.position = "absolute";
    a[0].style.zIndex = 10002;
    document.body.style.position = "static";
    document.body.appendChild(a[0]);
    list.push(a);
    $("#fanyiContainer").mouseover(function() {
        inFrame = true;
    }).mouseout(function() {
        inFrame = false;
    });
    $("#closeFrame").hover(function() {
        $(this).attr("src", chrome.extension.getURL("imgs/map/close_hover.png"));
    },
    function() {
        $(this).attr("src", chrome.extension.getURL("imgs/map/close.png"));
    });
    if (lastFrame != null) {
        if (lastFrame.css("left") !== $button.css("left")) {
            document.body.removeChild(lastFrame[0]);
        }
    }
    lastFrame = a;
    $("#closeFrame").click(function() {
        inFrame = false;
        $("#fanyiContainer").hide();
    });
    $("#moreMean").click(function() {
        inFrame = false;
        $("#fanyiContainer").hide();
    });
    $("#playVoice").hover(function() {
        if (!isPlayAudio) {
            $(this).attr("src", chrome.extension.getURL("imgs/map/voice_hover.png"));
        }
    },
    function() {
        if (!isPlayAudio) {
            $(this).attr("src", chrome.extension.getURL("imgs/map/voice.png"));
        }
    });
    $("#playVoice").click(function() {
        var j = this;
        isPlayAudio = true;
        $(this).siblings("audio")[0].play();
        var i = $(this).siblings("audio");
        i[0].play();
        $(this).attr("src", chrome.extension.getURL("imgs/map/sound.gif"));
        i.unbind("ended").bind("ended",
        function() {
            isPlayAudio = false;
            $(j).attr("src", chrome.extension.getURL("imgs/map/voice.png"));
        });
    });
}
function createFyBtn(h, g, e, d) {
    var a = $("<div></div>");
    a.attr("id", "fanyiWrapper");
    a.html("译");
    a.css({
        height: "32px",
        width: "33px",
        "font-family": "微软雅黑",
        "font-size": "14px",
        "text-align": "center",
        "line-height": "32px",
        color: "#fff",
        "background-color": "#4395FF",
        "border-radius": "2px",
        cursor: "pointer",
        "z-index": "99999"
    });
    var j = 35;
    var l = 35;
    var f = 0;
    var i = 0;
    var b = screen.availWidth;
    var c = screen.availHeight;
    var k = 10;
    if (e + j < b) {
        f = h;
    }
    a[0].style.left = f + "px";
    i = g;
    a[0].style.top = i + 10 + "px";
    a[0].style.position = "absolute";
    document.body.style.position = "static";
    document.body.appendChild(a[0]);
    $("#fanyiWrapper").mouseover(function() {
        inBtn = true;
    }).mouseout(function() {
        inBtn = false;
    });
    list.push(a);
    if (lastbutton !== null) {
        if (lastbutton.css("left") !== a.css("left")) {
            document.body.removeChild(lastbutton[0]);
        }
    }
    lastbutton = a;
}
function onCloseFrame() {
    if (inFrame) {
        return;
    }
    if (lastFrame != null) {
        while (list.length != 0) {
            document.body.removeChild(list.pop()[0]);
        }
        lastFrame = null;
        return true;
    }
    return false;
}
function onClosebutton() {
    if (inBtn) {
        return;
    }
    if (lastbutton != null) {
        while (list.length != 0) {
            document.body.removeChild(list.pop()[0]);
        }
        lastbutton = null;
        return true;
    }
    return false;
}